package com.heima.JWTTest.web;

import com.heima.JWTTest.properties.JwtProperties;
import com.heima.JWTTest.utils.AppJwtUtil;
import com.heima.JWTTest.utils.JwtUtil;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class JwtWeb {
    @Autowired
    private JwtProperties jwtProperties;

    @GetMapping("demo")
    public void demo(){
        Map map = new HashMap();
        map.put("id",1);
        String jwt = JwtUtil.createJWT(jwtProperties.getKey(), jwtProperties.getTtl(), map);
        System.out.println(jwt);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Claims claims = JwtUtil.getClaims(jwtProperties.getKey(), jwt);

//        Object id = claims.get("id");
        System.out.println(claims);

    }

    @GetMapping("test")
    public void test(){
        String jwt = AppJwtUtil.getToken(1l);
        System.out.println(jwt);
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Claims claimsBody = AppJwtUtil.getClaimsBody(jwt);
        int i = AppJwtUtil.verifyToken(claimsBody);
        System.out.println(i);
//        Claims claimsBody = AppJwtUtil.getClaimsBody(token);

    }
}
